package com.tiduyun.data.exchange.util;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.tiduyun.data.exchange.constant.ExceptionCode;
import com.tiduyun.data.exchange.exception.CodedRuntimeException;

/**
 * 参数校验通用方法工具类。
 * 
 * @author Wu.Zheng
 */
public class ParamCheckUtils {

    public static void notNull(Object obj, String paramName) throws CodedRuntimeException {
        notNull(obj, ExceptionCode.INVALID_PARAMETER, StringUtils.join("parameter \"", paramName, "\" is required"));
    }

    public static void notNull(Object obj, String exceptionCode, String msg) throws CodedRuntimeException {
        if (obj == null) {
            buildException(exceptionCode, msg);
        }
    }

    public static void notEmpty(Object[] objs, String paramName) throws CodedRuntimeException {
        notEmpty(objs, ExceptionCode.INVALID_PARAMETER, StringUtils.join("parameter \"", paramName, "\" must not be empty"));
    }

    public static void notEmpty(Object[] objs, String exceptionCode, String msg) throws CodedRuntimeException {
        if (ArrayUtils.isEmpty(objs)) {
            buildException(exceptionCode, msg);
        }
    }

    public static void notEmpty(Map<?, ?> objs, String paramName) throws CodedRuntimeException {
        notEmpty(objs, ExceptionCode.INVALID_PARAMETER, StringUtils.join("parameter \"", paramName, "\" must not be empty"));
    }

    public static void notEmpty(Map<?, ?> objs, String exceptionCode, String msg) throws CodedRuntimeException {
        if (MapUtils.isEmpty(objs)) {
            buildException(exceptionCode, msg);
        }
    }

    public static void notEmpty(List<?> objList, String paramName) throws CodedRuntimeException {
        notEmpty(objList, ExceptionCode.INVALID_PARAMETER, StringUtils.join("parameter \"", paramName, "\" must not be empty"));
    }

    public static void notEmpty(List<?> objList, String exceptionCode, String msg) throws CodedRuntimeException {
        if (CollectionUtils.isEmpty(objList)) {
            buildException(exceptionCode, msg);
        }
    }

    public static void notEmptyString(String str, String paramName) throws CodedRuntimeException {
        notEmptyString(str, ExceptionCode.INVALID_PARAMETER, StringUtils.join("parameter \"", paramName, "\" must not be empty"));
    }

    public static void notEmptyString(String str, String exceptionCode, String msg) throws CodedRuntimeException {
        if (org.apache.commons.lang3.StringUtils.isEmpty(str)) {
            buildException(exceptionCode, msg);
        }
    }

    public static void valueAllow(Object[] allows, Object value, String paramName) throws CodedRuntimeException {
        valueAllow(allows, value, ExceptionCode.INVALID_PARAMETER, StringUtils.join("parameter \"", paramName, "\" invalid"));
    }

    public static void valueAllow(Object[] allows, Object value, String exceptionCode, String msg) throws CodedRuntimeException {
        if (!ArrayUtils.contains(allows, value)) {
            buildException(exceptionCode, msg);
        }
    }

    private static void buildException(String exceptionCode, String msg) throws CodedRuntimeException {
        throw new CodedRuntimeException(exceptionCode, msg);
    }

}
